﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace SplenetiX
{
    
    public class Ennemi : Character
    {

        /*DEGEUG ENNEMIE*/
        static public SpriteFont Font;
        string text;
        /*\DEGEUG ENNEMIE*/

        int direction;
        float mvtSpeed, health, timerShoot, timerFollow;
        //List<Projectile> projectiles;
        bool isFollowing;
        //Vector2 vitesse;

        public float Health { get { return health; } }
        //public List<Projectile> Projectiles { get { return projectiles; } }
        public int Direction { get { return direction; } set { direction = value; } }
        public bool IsFollowing { get { return isFollowing; } set { isFollowing = value; } }

        //Variable animation ennemie
        SpriteAnimation AnimEnnemi;
        AnimationClass ani_ennemi = new AnimationClass();
        AnimationClass ani_ennemi2 = new AnimationClass();

        //Constructeur
        public Ennemi()
            : base(new Vector2(0, 0))
        { }


        public Ennemi(Vector2 position, float health)
            : base(position, CharacterType.Ennemi)
        {
            mvtSpeed = 0.1f;
            this.health = health;
            //projectiles = new List<Projectile>();
            timerShoot = 0f;
            direction = -1;
            isFollowing = false;
            timerFollow = Constant.FollowInterval;
            vitesse = new Vector2(0, 0);
        }

        public void Deplacement(Personnage joueur, GameTime gameTime)
        {
            //Joueur a portee

            if (timerFollow > Constant.FollowInterval && Math.Abs(joueur.Position.X - Position.X) < Constant.DistanceDeplacement + Constant.DistanceEngagement && Math.Abs(position.Y + texture.Height - joueur.Position.Y) < Constant.TileSize * 3)
            {
                direction = (joueur.Position.X - Position.X > 0) ? 1 : -1;
                isFollowing = true;
            }
            else
            {
                timerFollow += gameTime.ElapsedGameTime.Milliseconds;
                isFollowing = false;
                if (Position.X <= 0 || Position.X >= Constant.LevelRight - Texture.Width)
                {
                    direction = -direction;
                }
            }

            base.UpdatePosition(new Vector2(direction * mvtSpeed * gameTime.ElapsedGameTime.Milliseconds, 0));
            base.Move(gameTime.ElapsedGameTime.Milliseconds * 0.01f);
        }

        public void Load(ContentManager content)
        {
            /*DEGEUG ENNEMIE*/
            Font = content.Load<SpriteFont>("Hud_Interface//hudFont");
            /*\DEGEUG ENNEMIE*/

            this.LoadContent(content, "Ennemi//bowser");
            this.Position = new Vector2(this.Position.X, this.Position.Y - this.Texture.Height + Constant.TileSize);

            AnimEnnemi = new SpriteAnimation(content.Load<Texture2D>("Ennemi//ennemi"), 4, 1);

            AnimEnnemi.FramesPerSecond = 8;

            AnimEnnemi.AddAnimation("Left", 1, 4, ani_ennemi.Copy());
            AnimEnnemi.AddAnimation("LeftIdle", 1, 1, ani_ennemi.Copy());

            ani_ennemi.SpriteEffect = SpriteEffects.FlipHorizontally;
            AnimEnnemi.AddAnimation("Right", 1, 4, ani_ennemi.Copy());
            AnimEnnemi.AddAnimation("RightIdle", 1, 1, ani_ennemi.Copy());

        }


        public void Update(GameTime gameTime, Personnage player, Level L)
        {
            //Update de lanimation
            if (this.Direction == 1)
            {
                if (AnimEnnemi.Animation != "Right")
                    AnimEnnemi.Animation = "Right";
            }
            else
            {
                if (AnimEnnemi.Animation != "Left")
                    AnimEnnemi.Animation = "Left";
            }
            AnimEnnemi.Update(gameTime);

            //atack player
            if (!player.IsDie)
            {
                Hurt(player);
                Attack(gameTime, player);
            }

            //projectils updatings
            for (int i = 0; i < projectiles.Count; i++)
            {
                if (projectiles[i].Distance >= Constant.MaxRange)
                {
                    projectiles.RemoveAt(i);
                }
                else
                {
                    projectiles[i].Update(gameTime);
                }
            }

            /*DEGEUG ENNEMIE*/
            if (((int)(this.Position.X / 32)) > 1 && ((int)(this.Position.X / 32)) < ((L.Longueur) - 1) && ((int)(this.position.Y / 32) < (L.Hauteur - 1)))
            {
                text = "D: " + direction + " X: " + (int)((this.position.X / 32)) + " ; Y: " + (int)((this.position.Y / 32)+1);
                text += " Isfolow: " + IsFollowing;
                text += "\n bisouxDroit[" + L.bisouxchar[(int)(this.position.X / 32) + 1, (int)(this.position.Y / 32) + 1] + "]";
                text += "\n bisouxGauche[" + L.bisouxchar[(int)(this.position.X / 32) - 1, (int)(this.position.Y / 32) + 1] + "]";                
            }
            else
            {
                // out of bound
                text = "D: " + direction + " X: " + (int)((this.position.X / 32)) + " ; Y: " + (int)((this.position.Y / 32)+1);
            }
            /*\DEGEUG ENNEMIE*/

        }

        public void DrawEnnemi(SpriteBatch spriteBatch)
        {
            /*DEGEUG ENNEMIE*/
            //spriteBatch.DrawString(Font,text,new Vector2(this.position.X-10,this.position.Y-15),Color.White);
            /*\DEGEUG ENNEMIE*/
            AnimEnnemi.Draw(spriteBatch, new Vector2(this.Position.X + 8, this.Position.Y));
            for (int j = 0; j < this.Projectiles.Count; j++)
            {
                this.Projectiles[j].Draw(spriteBatch);
            }
        }

        //Calcule les degats selon le type d'attaque et les retranche a la vie de l'ennemi
        private void TakeDamage(int type)
        {
            switch (type)
            {
                default:
                    health -= Constant.BasicDamage;
                    break;
            }
        }

        //Verifie si l'ennemi est blesse
        private void Hurt(Personnage player)
        {
            for (int i = 0; i < player.Projectiles.Count; i++)
            {
                if (rectangle.Intersects(player.Projectiles[i].Rectangle))
                {
                    TakeDamage(0);
                    player.Projectiles.RemoveAt(i);
                }
            }
        }

        private void Attack(GameTime gameTime, Personnage player)
        {
            int distance = (int)(position.X - player.Position.X);
            //Si le joueur est suffisament pres de l'ennemi
            if (isFollowing && Math.Abs(distance) < Constant.DistanceEngagement && Math.Abs(position.Y - player.Position.Y) < 40)
            {
                //Direction du tir
                int coef;
                if (distance > 0)
                    coef = -1;
                else
                    coef = 1;

                Random rand = new Random();

                if (timerShoot >= Constant.ShootInterval * 2f && rand.Next(100) < 5)
                {
                    Shoot(coef);
                    timerShoot = 0;
                    if(Constant.music)
                        SoundEngine.PlaySound(SoundEngine.Tir);
                }
                else
                    timerShoot += gameTime.ElapsedGameTime.Milliseconds;
            }
        }

        public override void StopFollow()
        {
            if (!IsFollowing)
            {
                direction = -direction;
                //isFollowing = false;
                //timerFollow = 0;
            }
            
        }

        public override void IA(Level level) //if is Following
        {
            if (IsFollowing)
            {
                int X = (int)(this.position.X/32);
                int Y = (int)(this.position.Y/32);

                if (((int)(this.Position.X / 32)) > 1 && (((int)(this.Position.X / 32)) < ((level.Longueur) - 1)) && ((int)(this.position.Y/32)<(level.Hauteur-1)))
                {
                    if (Direction == -1)
                    {
                        if (level.bisouxchar[(int)(this.position.X / 32) , (int)(this.position.Y / 32) + 1] == '#' || level.bisouxchar[(int)(this.position.X / 32) , (int)(this.position.Y / 32) + 1] == 'S')
                        {
                            Jump();
                        }
                    }
                    else
                    {
                        if (level.bisouxchar[(int)(this.position.X / 32) + 1, (int)(this.position.Y / 32) + 1] == '#' || level.bisouxchar[(int)(this.position.X / 32) + 1, (int)(this.position.Y / 32) + 1] == 'S')
                        {
                            Jump();
                        }
                    }
                }  
            }
        }

        //Tir de projectile
        private void Shoot(int direction)
        {
            projectiles.Add(new Projectile(new Vector2(position.X, position.Y + 0.5f * texture.Height), direction));
        }
    }
}
